////package org.example.controller;
////
////import org.example.serve.ImageStorageService;
////import org.example.serve.OnnxModelService;
////import org.springframework.http.ResponseEntity;
////import org.springframework.web.bind.annotation.PostMapping;
////import org.springframework.web.bind.annotation.RequestParam;
////import org.springframework.web.bind.annotation.RestController;
////import org.springframework.web.multipart.MultipartFile;
////
////@RestController
////public class ImageRecognitionController {
////    private final OnnxModelService modelService;
////    private final ImageStorageService storageService;
////
////    public ImageRecognitionController(OnnxModelService modelService,
////                                      ImageStorageService storageService) {
////        this.modelService = modelService;
////        this.storageService = storageService;
////    }
////
////    @PostMapping("/api/recognize")
////    public ResponseEntity<String> recognizeAndStore(@RequestParam("image") MultipartFile file) {
////        try {
////            byte[] imageBytes = file.getBytes();
////            if (modelService.containsUVA(imageBytes)) {
////                storageService.saveUVAImage(imageBytes);
////                return ResponseEntity.ok("UVA detected and image saved.");
////            }
////            return ResponseEntity.ok("No UVA detected.");
////        } catch (Exception e) {
////            return ResponseEntity.status(500)
////                    .body("Error processing image: " + e.getMessage());
////        }
////    }
////}
//package org.example.controller;
//
//import org.example.serve.ImageStorageService;
//import org.example.serve.OnnxModelService;
//import org.springframework.http.ResponseEntity;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.bind.annotation.RestController;
//import org.springframework.web.multipart.MultipartFile;
//
//import javax.imageio.ImageIO;
//import java.awt.*;
//import java.awt.image.BufferedImage;
//import java.io.ByteArrayInputStream;
//import java.io.ByteArrayOutputStream;
//import java.io.IOException;
//
//@RestController
//public class ImageRecognitionController {
//    private final OnnxModelService modelService;
//    private final ImageStorageService storageService;
//
//    public ImageRecognitionController(OnnxModelService modelService,
//                                      ImageStorageService storageService) {
//        this.modelService = modelService;
//        this.storageService = storageService;
//    }
//
//    @PostMapping("/api/recognize")
//    public ResponseEntity<String> recognizeAndStore(@RequestParam("image") MultipartFile file) {
//        try {
//            byte[] imageBytes = file.getBytes();
//            // 调用缩放方法，这里假设指定大小为 640x640
//            byte[] resizedImageBytes = resizeImage(imageBytes, 640, 640);
//
//            if (modelService.containsUVA(resizedImageBytes)) {
//                storageService.saveUVAImage(resizedImageBytes);
//                return ResponseEntity.ok("UVA detected and image saved.");
//            }
//            return ResponseEntity.ok("No UVA detected.");
//        } catch (Exception e) {
//            return ResponseEntity.status(500)
//                    .body("Error processing image: " + e.getMessage());
//        }
//    }
//
//    private byte[] resizeImage(byte[] imageBytes, int width, int height) throws IOException {
//        // 将字节数组转换为 BufferedImage
//        ByteArrayInputStream bis = new ByteArrayInputStream(imageBytes);
//        BufferedImage originalImage = ImageIO.read(bis);
//
//        // 创建一个新的 BufferedImage 用于存储缩放后的图像
//        BufferedImage resizedImage = new BufferedImage(width, height, originalImage.getType());
//        Graphics2D g2d = resizedImage.createGraphics();
//        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
//        g2d.drawImage(originalImage, 0, 0, width, height, null);
//        g2d.dispose();
//
//        // 将缩放后的 BufferedImage 转换为字节数组
//        ByteArrayOutputStream bos = new ByteArrayOutputStream();
//        ImageIO.write(resizedImage, "jpg", bos);
//        return bos.toByteArray();
//    }
//}